/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import DTO.xmlobject.Products.Product;
import DTO.xmlobject.Products.Products;

/**
 *
 * @author ASUS
 */
public class ProductDAO extends Dataconnection{
    private Product ReadProductFromDS(ResultSet rs) {
        Product result = new Product();
        if (rs != null) {
            try {
                result.setProductID(rs.getLong("ProductID"));
                result.setTittle(rs.getString("Title"));
                result.setDescription(rs.getString("Description"));
                result.setPrice(rs.getFloat("Price"));
                result.setIsActive(rs.getBoolean("IsActive"));
                result.setIsHired(rs.getBoolean("IsHired"));
                result.setViews(rs.getLong("Views"));
                result.setHiredDate(rs.getDate("HiredDate"));
                result.setHiredPrice(rs.getInt("HiredPrice"));
                result.setDuongTruocNha(rs.getString("DuongTruocNha"));
                result.setNgan(rs.getFloat("Ngan"));
                result.setDai(rs.getFloat("Dai"));
                result.setSanthuong(rs.getBoolean("Santhuong"));
                result.setSoLau(rs.getLong("SoLau"));
                result.setSoPhongNgu(rs.getLong("SoPhongNgu"));
                result.setSoPhongTam(rs.getLong("SoPhongTam"));
                result.setContactId(rs.getLong("ContactId"));
                result.setDistrictID(rs.getLong("DistrictID"));
                result.setIsDelete(rs.getBoolean("IsDelete"));
                result.setCreatedDate(rs.getDate("CreatedDate"));
                result.setCreatedBy(rs.getString("CreatedBy"));
                result.setUpdatedDate(rs.getDate("UpdatedDate"));
                result.setUpdatedBy(rs.getString("UpdatedBy"));
            } catch (SQLException ex) {
                Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public Product getProductById(int id) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spProductGet(?) }");
            proc_stmt.setLong("ProductID", id);
            rs = proc_stmt.executeQuery();
            if (rs.next()) {
                return ReadProductFromDS(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Products getAllProduct() {
        Products result = new Products();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spProductGetList() }");
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getProduct().add(ReadProductFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public Product Create(Product value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spProductInsert(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("ProductID", java.sql.Types.INTEGER);
            proc_stmt.setString("Title", value.getTittle());
            proc_stmt.setString("Description", value.getDescription());
            proc_stmt.setFloat("Price", value.getPrice());
            proc_stmt.setBoolean("IsActive", value.isIsActive());
            proc_stmt.setBoolean("IsHired", value.isIsHired());
            proc_stmt.setLong("Views", value.getViews());
            proc_stmt.setDate("HiredDate", value.getHiredDate());
            proc_stmt.setFloat("HiredPrice", value.getHiredPrice());
            proc_stmt.setString("DuongTruocNha", value.getDuongTruocNha());
            proc_stmt.setFloat("Ngan", value.getNgan());
            proc_stmt.setFloat("Dai", value.getDai());
            proc_stmt.setBoolean("Santhuong", value.isSanthuong());
            proc_stmt.setLong("SoLau", value.getSoLau());
            proc_stmt.setLong("SoPhongNgu", value.getSoPhongNgu());
            proc_stmt.setLong("SoPhongTam", value.getSoPhongTam());            
            proc_stmt.setLong("ContactId", value.getContactId());
            proc_stmt.setLong("DistrictID", value.getDistrictID());
            proc_stmt.setBoolean("IsDelete", value.isIsDelete());
            proc_stmt.setDate("CreatedDate", value.getCreatedDate());
            proc_stmt.setString("CreatedBy", value.getCreatedBy());
            proc_stmt.setDate("UpdatedDate", value.getUpdatedDate());
            proc_stmt.setString("UpdatedBy", value.getUpdatedBy());
            proc_stmt.execute();
            value.setProductID(proc_stmt.getLong("ProductID"));
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return value;
    }

    public Product Edit(Product value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spProductUpdate(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("ProductID", java.sql.Types.INTEGER);
            proc_stmt.setString("Title", value.getTittle());
            proc_stmt.setString("Description", value.getDescription());
            proc_stmt.setFloat("Price", value.getPrice());
            proc_stmt.setBoolean("IsActive", value.isIsActive());
            proc_stmt.setBoolean("IsHired", value.isIsHired());
            proc_stmt.setLong("Views", value.getViews());
            proc_stmt.setDate("HiredDate", value.getHiredDate());
            proc_stmt.setFloat("HiredPrice", value.getHiredPrice());
            proc_stmt.setString("DuongTruocNha", value.getDuongTruocNha());
            proc_stmt.setFloat("Ngan", value.getNgan());
            proc_stmt.setFloat("Dai", value.getDai());
            proc_stmt.setBoolean("Santhuong", value.isSanthuong());
            proc_stmt.setLong("SoLau", value.getSoLau());
            proc_stmt.setLong("SoPhongNgu", value.getSoPhongNgu());
            proc_stmt.setLong("SoPhongTam", value.getSoPhongTam());            
            proc_stmt.setLong("ContactId", value.getContactId());
            proc_stmt.setLong("DistrictID", value.getDistrictID());
            proc_stmt.setBoolean("IsDelete", value.isIsDelete());
            proc_stmt.setDate("CreatedDate", value.getCreatedDate());
            proc_stmt.setString("CreatedBy", value.getCreatedBy());
            proc_stmt.setDate("UpdatedDate", value.getUpdatedDate());
            proc_stmt.setString("UpdatedBy", value.getUpdatedBy());
            proc_stmt.execute();
            return value;
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public boolean Delete(int value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spProductDelete(?) }");
            proc_stmt.setLong("ProductID", value);
            return proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public Products Search(String name) {
        Products result = new Products();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spProductSearch(?) }");
            proc_stmt.setString("ProductName", name);
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getProduct().add(ReadProductFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

}
